package cn.skyisazure.metacompare.metaVo;

import cn.skyisazure.metacompare.sqlcompare.exceptions.MetaCompareException;
import lombok.Data;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author wangjj
 * @date 2025/3/20 下午2:27
 */
@Data
public class EntityTableInfoVO {
    /** 基本信息 */
    private MetaTableRespVO metaTable;
    /** 字段集合 */
    private List<MetaColumnRespVO> fields;
    /** 验证器 */
    private List<MetaColumnValidatorRespVO> validators;
    /** 索引设置 */
    private List<MetaIndexRespVO> indexes;
    /** 外键关系设置 */
    private List<RelationRespVO> relations;
    /** 删除的关系 */
    private Set<String> removeRelations = new HashSet<>();

    /**获取[主键]字段名称*/
    public MetaColumnRespVO gainPrimaryField(){
        return this.fields.stream().filter(o->o.getSystemFieldType().equals(SystemColumnTypeEnum.PRIMARY_KEY)).findFirst().orElseThrow(()-> new MetaCompareException("表名："+this.metaTable.getCode()+"没有主键"));
    }
    /**获取[删除标志]字段名称*/
    public MetaColumnRespVO gainLogicDeleteField(){
        return this.fields.stream().filter(o->o.getSystemFieldType().equals(SystemColumnTypeEnum.DELETE_FLAG)).findFirst().orElseThrow(()-> new MetaCompareException("表名："+this.metaTable.getCode()+"没有删除标志"));
    }
}
